package stef;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Vector;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;


public class FileScrollPanel extends JPanel implements KeyListener, ActionListener{

	private static final long serialVersionUID = 1L;

	private JTable table;

	public FileScrollPanel(){
		setLayout(new BorderLayout());
		JLabel label = new JLabel(" ");
		label.setPreferredSize(new Dimension(300, 30));



		DefaultTableModel model = new DefaultTableModel(){
			private static final long serialVersionUID = 1L;
			@Override
			public boolean isCellEditable(int row, int column){
				return false;
			}
		};
		model.addColumn("File name");

		table = new JTable();
		table.setModel(model);

		table.addKeyListener(this);
		
		add(label, BorderLayout.NORTH);
		add(new JScrollPane(table), BorderLayout.CENTER);
		
		
		Container c = new Container();
		c.setLayout(new BoxLayout(c, BoxLayout.Y_AXIS));
		JButton deleteSelected = new JButton("Delete selected");
		deleteSelected.setMaximumSize(new Dimension(Integer.MAX_VALUE, 30));
		deleteSelected.addActionListener(this);
		
		deleteSelected.setForeground(Color.RED);
		c.add(deleteSelected);
		
		label = new JLabel(" ");
		label.setMaximumSize(new Dimension(Integer.MAX_VALUE, 30));
		c.add(label);
		
		add(c, BorderLayout.SOUTH);

	}



	public void addData(List<GPSFile> list){
		DefaultTableModel model = (DefaultTableModel) table.getModel();

		for(GPSFile file: list){
			model.addRow(new Vector<>(Arrays.asList(file)));
		}

	}



	public List<GPSFile> getFiles() {
		List<GPSFile> files = new ArrayList<>();
		for(int i=0; i<table.getRowCount(); i++){
			for(int j=0; j<table.getColumnCount(); j++){
				files.add((GPSFile) table.getModel().getValueAt(i, j));
			}
		}
		return files;
	}



	@Override
	public void keyPressed(KeyEvent e) {

	}



	@Override
	public void keyReleased(KeyEvent e) {
		int keyCode = e.getKeyCode();
		if(keyCode == KeyEvent.VK_DELETE){
			deleteSelectedFiles();
		}
	}



	@Override
	public void keyTyped(KeyEvent e) {

	}



	@Override
	public void actionPerformed(ActionEvent e) {
		deleteSelectedFiles();
	}

	
	public void deleteSelectedFiles(){
		int[] rows = table.getSelectedRows();
		if(rows.length == 0){
			return;
		}
		String message = "Are you sure you don't want to transfer the following files?\n\n";
		for(int x: rows){
			message += table.getValueAt(x, 0) + "\n";
		}

		int result = JOptionPane.showConfirmDialog(null, message);

		if(result==JOptionPane.YES_OPTION){
			DefaultTableModel model = (DefaultTableModel) table.getModel();
			int i = 0;
			for(int x: rows){
				model.removeRow(x - i);
				i++;
			}
		}
	}
}
